using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.ModelParam.Input;
//using HuiLian.Service.Platform.ModelParam.Output;
using HuiLian.Common.Attributes;
using HuiLian.Common;
using HuiLian.Service.Platform.ModelParam.Output;
using Microsoft.AspNetCore.Mvc;

namespace HuiLian.Service.Platform.ModelParam
{
    /// <summary>
    /// 模型参数服务
    /// </summary>
    public class ModelParamService : IModelParamService
    {
        private readonly IMapper _mapper;
        private readonly IModelParamRepository _modelParamRepository;

        public ModelParamService(IMapper mapper, IModelParamRepository modelParamRepository)
        {
            _mapper = mapper;
            _modelParamRepository = modelParamRepository;
        }

        /// <summary>
        /// 获取模型参数信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync()
        {
            var result = await _modelParamRepository.Select.FirstAsync();
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询模型参数信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<ModelParamEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _modelParamRepository.Select
            //.WhereDynamicFilter(input.GetDynamicFilter<DynamicFilterInfo>())
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync();

            var data = new PageOutput<ModelParamEntity>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 添加模型参数信息
        /// </summary>
        /// <param name="input">模型参数信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(ModelParamAddInput input)
        {
            var entity = _mapper.Map<ModelParamEntity>(input);
            var id = (await _modelParamRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新模型参数信息
        /// </summary>
        /// <param name="input">模型参数信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(ModelParamUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _modelParamRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _modelParamRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除模型参数信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _modelParamRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除模型参数信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _modelParamRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软删除模型参数信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _modelParamRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 获取管网健康度的图例
        /// </summary>
        /// <returns></returns>
        public async Task<IResponseOutput> PipeHealthLegend()
        {
            double weiXianYuzhi = 0.9;
            var modelParam = await _modelParamRepository.Select.FirstAsync();
            if (modelParam != null)
            {
                weiXianYuzhi = modelParam.threshold;
            }
            List<PipeHealthLegendOutput> result = new List<PipeHealthLegendOutput>();
            result.Add(new PipeHealthLegendOutput { Name = "未知", Min = -2, Max = -1, Label = "-1.0--2.0" });
            result.Add(new PipeHealthLegendOutput { Name = "健康", Min = 0, Max = 0.5, Label = "0.0-0.5" });
            result.Add(new PipeHealthLegendOutput { Name = "一般", Min = 0.5, Max = 0.75, Label = "0.5-0.75" });
            result.Add(new PipeHealthLegendOutput { Name = "较差", Min = 0.75, Max = weiXianYuzhi, Label = $"0.75-{weiXianYuzhi}" });
            result.Add(new PipeHealthLegendOutput { Name = "危险", Min = weiXianYuzhi, Max = 1, Label = $"{weiXianYuzhi}-1.0" });
            
            return ResponseOutput.Ok(result);
        }
    }
}
